package com.bw.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bw.finance.eneity.TbMenu;
import com.bw.finance.eneity.res.TbMenuRes;
import com.bw.finance.mapper.TbMenuMapper;
import com.bw.finance.mapper.TbRoleMenuMapper;
import com.bw.finance.service.ITbMenuService;
import com.bw.finance.utils.Tree;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lizhengqian
 * @since 2020-05-09
 */
@Service
public class TbMenuServiceImpl extends ServiceImpl<TbMenuMapper, TbMenu> implements ITbMenuService {
    @Autowired
    private TbMenuMapper menuMapper;
    @Autowired
    private TbRoleMenuMapper roleMenuMapper;
    @Override
    public List<TbMenuRes> findlist() {
        List<TbMenuRes> tbMenuReslist=new ArrayList<TbMenuRes>();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.orderByAsc("ORDER_NUM");
        List<TbMenu> tbmenulist = menuMapper.selectList(queryWrapper);
        for (TbMenu n:tbmenulist) {
            TbMenuRes tbMenuRes=new TbMenuRes();
            tbMenuRes.setId(n.getId());
            tbMenuRes.setMenuName(n.getMenuName());
            tbMenuRes.setIcon(n.getIcon());
            tbMenuRes.setOrderNum(n.getOrderNum());
            tbMenuRes.setParentId(n.getParentId());
            tbMenuRes.setUrl(n.getUrl());
            tbMenuRes.setMenuType(n.getMenuType());
            tbMenuReslist.add(tbMenuRes);
        }
        return Tree.bulidtree(tbMenuReslist);
    }
    public List<TbMenuRes> bulidtree(List<TbMenuRes> list){
        List<TbMenuRes> tree=new ArrayList<TbMenuRes>();
        for (TbMenuRes n:list) {
            if(n.getParentId()==0){
                tree.add(findchild(n,list));
            }
        }
        return tree;
    }
    public TbMenuRes findchild(TbMenuRes node,List<TbMenuRes> list){
        for (TbMenuRes n:list) {
            if(n.getParentId().equals(node.getId())){
                if(node.getChildren()==null){
                    node.setChildren(new ArrayList<>());
                }
                node.getChildren().add(findchild(n,list));
            }
        }
        return node;
    }
    @Override
    @Transactional//开启事务
    public void remove(Long id) {
            menuMapper.deleteById(id);
            QueryWrapper queryWrapper=new QueryWrapper();
            queryWrapper.eq("MENU_ID",id);
            roleMenuMapper.delete(queryWrapper);
    }

    @Override
    public List<TbMenuRes> getTreeById(Long id) {
        List<TbMenuRes> tbMenuReslist=new ArrayList<TbMenuRes>();
        List<TbMenu> tbmenulist=menuMapper.getTreeById(id);
        for (TbMenu n:tbmenulist) {
            TbMenuRes tbMenuRes=new TbMenuRes();
            tbMenuRes.setId(n.getId());
            tbMenuRes.setMenuName(n.getMenuName());
            tbMenuRes.setIcon(n.getIcon());
            tbMenuRes.setOrderNum(n.getOrderNum());
            tbMenuRes.setParentId(n.getParentId());
            tbMenuRes.setUrl(n.getUrl());
            tbMenuRes.setMenuType(n.getMenuType());
            tbMenuReslist.add(tbMenuRes);
        }
        return Tree.bulidtree(tbMenuReslist);
    }
}
